package cn.edu.buaa.nlsde.grid.DatabaseManage;

import java.sql.*;

import cn.edu.buaa.nlsde.grid.DatabaseConnect.DBPool;
import cn.edu.buaa.nlsde.grid.Structure.User;

public class LoginManage
{
	DBPool dbpool=null;
	Statement stat=null;
	String sql=null;
	
	public LoginManage(DBPool dbpool)
	{
		this.dbpool=dbpool;
	}
	
	public String addUser(User user)
	{
		try
		{
			sql="insert into User_Info (UserID,UserName,pass_word) values ('"+user.getUserID()+"','"+user.getUserName()+"','"+user.getPassword()+"')";
			stat=dbpool.connect();
			stat.executeUpdate(sql);
			return "Success";
		}
		catch(SQLException e)
		{
			return "Fail";
		}
	}
	public String deleteUser(String userID)
	{
		try
		{
			sql="delete from User_Info where UserID='"+userID+"'";
			stat=dbpool.connect();
			stat.executeUpdate(sql);
			return "Success";
		}
		catch(SQLException e)
		{
			return "Fail";
		}
	}
	
	public String checkUser(User user)
	{
		
		try
		{
			sql="select * from User_Info where UserID='"+user.getUserID()+"'";
			stat=dbpool.connect();
			ResultSet rs=stat.executeQuery(sql);
			if(user.getUserID()!=null&&rs.next())
			{
				if(user.getPassword().equals(rs.getString("pass_word")))
				{
					return "Success";
				}
				else
				{
					return "Wrong";
				}
			}
			else
			{
				return null;
			}
		}
		catch(SQLException e)
		{
			return null;
		}
		
	}
	
	public void close() throws SQLException
	{
		dbpool.close();
	}

}
